"use strict";

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['elfinder'], factory);
    } else if (typeof exports !== 'undefined') {
        module.exports = factory(require('elfinder'));
    } else {
        factory(root.elFinder);
    }
}(this, function (elFinder) {
        elFinder.prototype.commands.moresetting = function () {
            this.exec = function (hashes, opts) {
                //implement what the custom command should do here
                var fm    = this.fm,
                    files = this.files(hashes),
                    cnt   = files.length,
                    thash = (typeof opts == 'object')? opts.thash : false,
                    opts  = this.options,
                    url ,
                    file = files[cnt-1];

                    if (!file.read) {
                        alert("sssssss");
                    }
                    url = this.openMoreUrl(file);
                    window.open(url);
            }
            this.getstate = function (sel) {
                //return 0 to enable, -1 to disable icon access
                var sel = this.files(sel),
                    cnt = sel.length;

                return (cnt == 1&&sel[0].mime != 'directory')
                    ? (sel[0].read? 0 : -1)
                    : -1
            }
            /**
             * Return file url for open in elFinder
             *
             * @param  String  file hash
             * @param  Boolean for download link
             * @return String
             */
            this.openMoreUrl = function(file) {
                var url  = '';

                if (!file || !file.read) {
                    return '';
                }

                url =   "/a/netdisk/diskInfo/fileinfo";
                url = url + (url.indexOf('?') === -1 ? '?' : '&')
                    + (this.oldAPI ? 'cmd=open&current='+file.phash : 'cmd=fileinfo')
                    + '&target=' + file.hash;
                $.each(this.options.customData, function(key, val) {
                    url += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(val);
                });

                return url;
            };
        }

    }
));
